package com.zj.osm.view.home

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.Window
import android.view.animation.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.zj.osm.App
import com.zj.osm.R
import com.zj.osm.view.home.home.HomeActivity
import com.zj.utils.utils.utilcode.AppUtils
import com.zj.utils.utils.utilcode.BarUtils
import com.zj.utils.utils.utilcode.SPUtils
import kotlinx.android.synthetic.main.activity_welcome.*


class WelcomeActivity : AppCompatActivity(), View.OnClickListener {
    private var exitTime: Long = 0
    private var animationTime: Long = 1000
    override fun onCreate(savedInstanceState: Bundle?) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        BarUtils.setStatusBarVisibility(window, false)
        super.onCreate(savedInstanceState)
        //requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setContentView(R.layout.activity_welcome)
        var timeEnd=SPUtils.getInstance().getLong("firstTime")
        if(timeEnd==-1L){
            SPUtils.getInstance().put("firstTime",System.currentTimeMillis())
        }
        initView()
    }

    private fun initAnimation() {
        //旋转
        val rotateAnimation = RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.duration = animationTime
        rotateAnimation.fillAfter = true
        //缩放1.开始X轴尺寸 2.结束X轴尺寸
        val scaleAnimation = ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        scaleAnimation.duration = animationTime
        scaleAnimation.fillAfter = true
        //渐变
        val alphaAnimation = AlphaAnimation(0f, 1f)
        alphaAnimation.duration = animationTime
        alphaAnimation.fillAfter = true
        //动画一起执行 ,组合动画   : 是否使用同一个动画插补器 true表示各个动画使用同一个插补器,false不使用一个
        val animationSet = AnimationSet(true)
        animationSet.addAnimation(alphaAnimation)
        //执行我们的动画
        rlWelcomeBg!!.startAnimation(animationSet)
        //动画执行完执行页面跳转操作  设置动画的结束监听
        animationSet.setAnimationListener(animationListener)


    }


    @SuppressLint("SetTextI18n")
    private fun initView() {
        initAnimation()
        rlWelcomeBg.setOnClickListener(this)
        welcomeTvVersion.text = "版本号:${AppUtils.getAppVersionName()}"
//        val time = System.currentTimeMillis()
//        val moduleTypeServiceImpl = ModuleMaterialsServiceImpl()
//        val moduleMaterialsServiceImpl = moduleTypeServiceImpl.getModuleMaterialsByModuleId("${-2000}")
//        Log.e("TAG", "saveMarker initView: equipTypeList   $moduleMaterialsServiceImpl")
//        Log.e("TAG", "saveMarker initView: 时间" + (System.currentTimeMillis() - time))
    }


    private val animationListener = object : Animation.AnimationListener {
        /**
         * 动画开始的时候执行
         * @param animation
         */
        override fun onAnimationStart(animation: Animation) {

        }

        /**
         * 动画结束的时候执行
         * @param animation
         */
        override fun onAnimationEnd(animation: Animation) {
            //跳转到登陆界面
            jump()
        }

        /**
         * 动画重复的时候执行
         * @param animation
         */
        override fun onAnimationRepeat(animation: Animation) {

        }
    }

    private fun jump() {
        startActivity(Intent(this, HomeActivity::class.java))
        finish()
    }


    override fun onClick(v: View?) {
        if (v != null) {
            when (v.id) {
                R.id.rlWelcomeBg -> {
                    jump()
                }
            }
        }
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }
    private fun exit() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            Toast.makeText(applicationContext, "再按一次退出程序",
                    Toast.LENGTH_SHORT).show()
            exitTime = System.currentTimeMillis()
            SPUtils.getInstance().put("update", "0")

        } else {
            App.getInstances().exit(1)
        }
    }
}
